//
//  ZCNetworkTools.swift
//  weibo
//
//  Created by Ning Xie on 16/8/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

enum RegestType: Int {
    case GET = 0
    case POST = 1
}
let AppKey: String = "3527870846"
let AppSecret: String = "1ef5b16b3dcae21c183c5260351e4076"
let redirect_uri: String = "http://www.itcast.cn"
let token = "2.00nLK3kBg3akqD367a2ddfe30qZ8EH"

class ZCNetworkTools: AFHTTPSessionManager
{
    //  闭包的别名
    typealias CallBackType = (response: AnyObject?, error: NSError?)->()

    
    static let sharedNetworkTools: ZCNetworkTools = {
        let tools = ZCNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    //封装GET/POST
    func request(regestType:RegestType,url:String,parameter:AnyObject?,callBack:CallBackType)
    {
        if regestType == .GET
        {
            GET(url, parameters: parameter, progress: nil, success: { (_, response:AnyObject?) in
                
                callBack(response: response, error: nil)
                
            }) { (_, error:NSError) in
                
                callBack(response: nil, error: error)
            }
        }
        else
        {
            POST(url, parameters: parameter, progress: nil, success: { (_, response:AnyObject?) in
                
                callBack(response: response, error: nil)
                
            }) { (_, error:NSError) in
                
                callBack(response: nil, error: error)
            }
        }
    
    }
    //封装图片上传
    func requestUpload(url:String,parameter:AnyObject?,imageData:NSData,name:String,callBack:CallBackType){
        
        POST(url, parameters: parameter, constructingBodyWithBlock: { (formData) in
            
            formData.appendPartWithFileData(imageData, name: name, fileName: "test", mimeType: "application/octet-stream")
            
            }, progress: nil, success: { (_, response) in
                callBack(response: response, error: nil)
        }) { (_, error) in
            callBack(response: nil, error: error)
        }
    }
}


extension ZCNetworkTools{
    
    //请求用户授权Token
    func requestOAuthLogin()->(NSURLRequest){
        
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(AppKey)&redirect_uri=\(redirect_uri)"
        let request = NSURLRequest(URL: NSURL(string: url)!)
        
        return request
        
    }
    
    //获取授权过的Access Token
    func requestAccessToken(code:String, callBack:CallBackType){
        
        let url = "https://api.weibo.com/oauth2/access_token"
        let parameter = ["client_id":AppKey,"client_secret":AppSecret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        request(.POST, url: url, parameter: parameter, callBack: callBack)
    }
    
    //根据用户ID获取用户信息
    func requestUserInfo(userInfo:ZCUserInfoModel,callBack:CallBackType){
        
        let url = "https://api.weibo.com/2/users/show.json"
        let parameter = ["access_token":userInfo.access_token!,"uid":"\(userInfo.uid)"]
        
        request(.GET, url: url, parameter: parameter, callBack: callBack)
    }
    
    //获取当前登录用户及其所关注（授权）用户的最新微博
    func requestStatuses(access_token:String,since_id:Int64 = 0,max_id:Int64 = 0,callBack:CallBackType){
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let parameter = ["access_token":access_token,"since_id":"\(since_id)","max_id":"\(max_id)"]
        
        request(.GET, url: url, parameter: parameter, callBack: callBack)
    }
    
    //        https://api.weibo.com/2/statuses/friends_timeline.json?access_token=2.00nLK3kBg3akqD367a2ddfe30qZ8EH
    
}

extension ZCNetworkTools{
    
    //发送一条文字微博
    func requestUpdate(access_token:String,status:String,callBack:CallBackType){
        let url = "https://api.weibo.com/2/statuses/update.json"
        let parameter = ["access_token":access_token,"status":status]
        
        request(.POST, url: url, parameter: parameter, callBack: callBack)
    }
    
    
    //发送一条带图片微博
    func requestUpload(access_token:String,status:String,image:UIImage,callBack:CallBackType){
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        let parameter = ["access_token":access_token,"status":status]
        
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        imageData.writeToFile("/Users/NingXIE/Desktop/1.jpeg", atomically: true)
        
        requestUpload(url, parameter: parameter, imageData: imageData, name: "pic", callBack: callBack)
        
    }
    
    
}



